1218. 最长定差子序列
https://leetcode-cn.com/problems/longest-arithmetic-subsequence-of-given-difference/
- 提交时间:2021-11-08 14:19:52
- 执行用时:120 ms, 在所有 Go 提交中击败了9.03%的用户
- 内存消耗:9.3 MB, 在所有 Go 提交中击败了38.25%的用户
- 通过测试用例:39 / 39
func longestSubsequence(arr []int, difference int) (ans int) {
dp := map[int]int{}
for _, num := range arr {
dp[num] = dp[num-difference] + 1
if dp[num] > ans {
ans = dp[num]
}
}
return
}